package com.kingland.springbootstarterdataredis.controller;

import com.kingland.springbootstarterdataredis.model.StudentModel;
import com.kingland.springbootstarterdataredis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 查询所有学生
     *
     * @return String for List<StudentModel> 所有学生
     */
    @GetMapping({"", "/", "index"})
    public String findAll() {
        return studentService.findAll().toString();
    }

    /**
     * 添加学生
     *
     * @param name
     * @param age
     * @return String for StudentModel 添加的学生
     */
    @GetMapping("/add/{name}/{age}")
    public String add(@PathVariable String name, @PathVariable Integer age) {
        StudentModel studentModel = new StudentModel();
        studentModel.setName(name);
        studentModel.setAge(age);
        studentModel = studentService.save(studentModel);
        return studentModel.toString();
    }

    /**
     * 使用id查看指定学生
     *
     * @param id
     * @return String for StudentModel 指定学生
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable Long id) {
        StudentModel studentModel = studentService.findById(id);
        return studentModel != null ? studentModel.toString() : "null";
    }

    /**
     * 使用id删除指定学生
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        studentService.deleteById(id);
        return "ok";
    }

    // TODO: 使用学生名字查找指定学生
    // TODO: 使用批量的学生id查找指定学生
    // TODO: 传输一个studentModel，查询是否存在相同的id，如果存在则更新，不存在则插入 - tips: @RequestBody, Content-Type: application/json {}
}
